﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Domain;
using DAO;

public partial class m_login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Utils.operadorLogueado != null)
                if (Utils.operadorLogueado is Usuario)
                    doRedirect();

            a_crearcuenta.HRef = "m_Registracion.aspx" + (string.IsNullOrEmpty(Request.QueryString.ToString()) ? "" : ("?"+Request.QueryString.ToString()));
        }
    }

    private void doRedirect()
    {
        // saneamiento

        long id = 0;
        if (Request.QueryString["id"] == null ||
            !long.TryParse(Request.QueryString["id"].ToString(), out id))
        {
            Response.Redirect(Utils.SecureRedirect("~/m_ManageSuscripciones_Mias.aspx"));
        }

        string page = null;
        if (Request.QueryString["t"] != null)
        {
            if (Request.QueryString["t"] == "e")
            {
                if (EmpresaDAO.Traer(id) == null)
                    Response.Redirect(Utils.SecureRedirect("~/m_ManageSuscripciones_Mias.aspx"));

                page = "m_EmpData.aspx";
            }
            else if (Request.QueryString["t"] == "p")
            {
                if (PublicacionDAO.Traer(id, null, null).Count() <= 0)
                    Response.Redirect(Utils.SecureRedirect("~/m_ManageSuscripciones_Mias.aspx"));

                page = "m_PublicacionData.aspx";
            }
        }

        Response.Redirect(Utils.SecureRedirect("~/" + page + "?id=" + id.ToString()));
    }

    protected void btn_login_Click(object sender, EventArgs e)
    {
        ul_errores.InnerHtml = string.Empty;

        long usr = 0;
        if (!long.TryParse(txt_username.Text, out usr))
        {
            ul_errores.InnerHtml += "<li class='itemerror'>" + "Debe ingresar un usuario válido" + "</li>";
            return;
        }

        if (string.IsNullOrEmpty(txt_pass.Text))
        {
            ul_errores.InnerHtml += "<li class='itemerror'>" + "Debe ingresar la contraseña" + "</li>";
            return;
        }

        Operador operador = null;
        
        Usuario user = UsuarioDAO.Traer(usr);
        if (user != null)
        {
            operador = user;
        }

        if (operador == null)
        {
            ul_errores.InnerHtml += "<li class='itemerror'>" + "El usuario no existe" + "</li>";
            return;
        }

        if (operador.Password != txt_pass.Text)
        {
            ul_errores.InnerHtml += "<li class='itemerror'>" + "Datos incorrectos" + "</li>";
            return;
        }

        if (chk_keeplogued.Checked)
        {
            DateTime now = DateTime.Now;
            HttpCookie cookieWrite = new HttpCookie("operador");

            // Set the cookie value.
            cookieWrite.Value = (operador is Usuario ? typeof(Usuario).ToString() : typeof(Empresa).ToString()) + "=" + operador.UserName;

            // Set the cookie expiration date.
            cookieWrite.Expires = now.AddDays(1);

            // Add the cookie.
            Response.Cookies.Add(cookieWrite);
        }

        Utils.operadorLogueado = operador;

        doRedirect();

    }
}
